<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>文件上传</title>
    <style>
      body {
        font-family: Arial, sans-serif;
        display: flex;
        justify-content: center;
        align-items: center;
        padding: 0;
        margin: 0;
      }
      .container {
        width: 100%;
        text-align: center;
        box-sizing: border-box;
        padding: 20px;
      }
      .file-label {
        width: 100%;
        border: 1px dashed #ccc;
        border-radius: 5px;
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        background-color: #fff;
        box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
        margin: 10px 0;
        padding: 20px 0;
      }
      .file-label:hover {
        background-color: #f0f0f0;
      }
      .file-label span {
        color: #888;
      }
      #fileInput {
        display: none;
      }
      input[type="text"] {
        padding: 10px;
        box-sizing: border-box;
        /* margin: 10px 0; */
        display: block;
        width: 100%;
      }
      button {
        width: 100%;
        padding: 10px;
        background: linear-gradient(150deg, #7a54df 0.234%, #675eef 5.57%, #417afc 34.642%, #56d3ff 100%);
        color: white;
        border: none;
        border-radius: 5px;
        cursor: pointer;
        opacity: 0.5;
      }
      button:disabled {
        cursor: not-allowed;
      }
      .progress-bar {
        width: 100%;
        background: #ddd;
        border-radius: 5px;
        overflow: hidden;
        margin-top: 10px;
        display: none;
      }
      .progress {
        height: 10px;
        width: 0;
        background: #12c2e9;
        background: -webkit-linear-gradient(to right, #12c2e9, #c471ed, #f64f59);
        background: linear-gradient(to right, #12c2e9, #c471ed, #f64f59);
      }
    </style>
  </head>
  <body>
    <div class="container">
      <input type="text" id="pathInput" placeholder="输入目录，如'D:\\'(默认下载电脑Downloads目录)" />

      <div class="progress-bar" id="progressBar">
        <div class="progress" id="progress"></div>
      </div>

      <label for="fileInput" class="file-label"><span id="fileLabel">上传文件</span></label>
      <input type="file" id="fileInput" onchange="handleFileChange()" />

      <button id="uploadBtn" onclick="uploadFile()" disabled>上传</button>

      <p id="status"></p>
    </div>

    <script>
      var url = window.location.href;
      var params = url.split("?")[1];
      var paramObj = {};
      if (params) {
        var paramArr = params.split("&");
        for (var i = 0; i < paramArr.length; i++) {
          var keyValue = paramArr[i].split("=");
          var key = keyValue[0];
          var value = keyValue[1];
          paramObj[key] = value;
        }
      }
      var key = atob(paramObj.v1) + atob(paramObj.v2);

      const uploadBtn = document.getElementById("uploadBtn");

      function handleFileChange() {
        const fileInput = document.getElementById("fileInput");
        const fileLabel = document.getElementById("fileLabel");
        const progressBar = document.getElementById("progressBar");

        if (fileInput.files.length > 0) {
          fileLabel.textContent = fileInput.files[0].name;
          uploadBtn.disabled = false;
          uploadBtn.style.opacity = "1";
          progressBar.style.display = "block";
        }
      }

      function uploadFile() {
        const fileInput = document.getElementById("fileInput");
        const pathInput = document.getElementById("pathInput");
        const progressBar = document.getElementById("progress");
        const status = document.getElementById("status");

        if (fileInput.files.length === 0) {
          alert("请选择文件");
          return;
        }
        fileInput.disabled = true;
        uploadBtn.disabled = true;
        uploadBtn.style.opacity = "0.5";

        const file = fileInput.files[0];
        const path = pathInput.value;
        const formData = new FormData();
        formData.append("file", file);
        formData.append("path", path);

        const xhr = new XMLHttpRequest();
        xhr.open("POST", `http://${paramObj.i}:${paramObj.p}/file/upload`, true);
        xhr.setRequestHeader("X-Access-Key", key);

        xhr.upload.onprogress = function (event) {
          if (event.lengthComputable) {
            let percent = (event.loaded / event.total) * 100;
            progressBar.style.width = percent + "%";
          }
        };

        xhr.onload = function () {
          if (xhr.status === 200) {
            status.innerText = "上传成功！";
          } else if (xhr.status === 400) {
            msg = JSON.parse(xhr.responseText).msg;
            status.innerText = msg;
          } else {
            status.innerText = "上传失败！";
          }

          fileInput.disabled = false;
          uploadBtn.disabled = false;
          uploadBtn.style.opacity = "1";
        };

        xhr.onerror = function () {
          status.innerText = "上传出错！";
          fileInput.disabled = false;
          uploadBtn.disabled = false;
          uploadBtn.style.opacity = "1";
        };

        xhr.send(formData);
      }
    </script>
  </body>
</html>
